package com.qiantu.phone.ui.activity;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.hjq.bar.TitleBar;
import com.hjq.shape.view.ShapeEditText;
import com.lib.migu.MiguManager;
import com.qiantu.api.db.LLControllerDBManager;
import com.qiantu.api.entity.ControllerBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.fragment.MusicListFragment;
import com.qiantu.phone.ui.fragment.MusicTextFragment;
import com.qiantu.phone.ui.fragment.MusiclikeListFragment;
import com.qiantu.phone.widget.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.List;

public class PlayMusicListActivity extends AppActivity implements MusicListFragment.MusicListInterface,MusicTextFragment.MusicTextInterface,MusiclikeListFragment.MusicLikeListInterface{
    private ViewPager music_view_pager;
    private List<Fragment> fragments = new ArrayList<>();
    private FragmentManager mFragmentManager;
    private ViewPagerIndicator tab_bar;
    private ShapeEditText edit_search;
    private FragmentStatePagerAdapter fragmentStatePagerAdapter;
    private String serialNo;
    private String controllerserialNo;
    private String songname;
    private String dialog_sign;
    private String music_name;
    private String musicID;
    private String musicName;
    private String musicPic;
    private String[] tabs;
    private TitleBar titlebar;
    private MiguManager miguManager = MiguManager.getInstance();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_play_music_list;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        serialNo = intent.getStringExtra("serialNo");
        songname = intent.getStringExtra("songname");
        controllerserialNo = intent.getStringExtra("controllerserialNo");
        dialog_sign = intent.getStringExtra("dialog_sign");
        music_view_pager = findViewById(R.id.music_view_pager);
        titlebar = findViewById(R.id.titlebar);
        edit_search = findViewById(R.id.edit_search);
        tab_bar = findViewById(R.id.tab_bar);
        tabs = new String[]{getResources().getString(R.string.Now_Playing), getResources().getString(R.string.I_like)};
        mFragmentManager = getSupportFragmentManager();
        if (dialog_sign!=null){
            if (dialog_sign.equals("deviceDialog")){
                titlebar.setRightTitle(R.string.save);
                titlebar.setRightTitleColor(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
                titlebar.setRightTitleSize(14);
            };
        }else {
            dialog_sign = "none_sign";
        }

        initViewPager(1);//1 音乐列表   0 搜索列表
        initedit();
        ControllerBean ctroller = LLControllerDBManager.getInstance(getContext()).get(controllerserialNo);
        miguManager.init(AppApplication.getInstance(),ctroller.getDeviceSn());//初始化咪咕音乐管理类
    }

    //搜索框监听
    private void initedit() {
        edit_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //根据输入框文字改变进入哪个fragment
                if (s.toString()==null || s.toString().equals("")){
                    tab_bar.setVisibility(View.VISIBLE);
                    initViewPager(1);
                }else {
                    music_name = s.toString();
                    tab_bar.setVisibility(View.GONE);
                    initViewPager(0);
                }

            }
        });
    }

    private int type_length = 0;
    private void initViewPager(int typeI) {
        fragments.clear();
        switch (typeI){
            case 0:
                //搜索界面只有一个fragment
                fragments.add(MusicTextFragment.newInstance(serialNo,music_name,dialog_sign));
                type_length = 1;
                break;
            case 1:
                //正常进入是两个fragment
                tab_bar.setupViewpager(music_view_pager);
                tab_bar.setTitles(tabs);
                fragments.add(MusicListFragment.newInstance(serialNo,songname,typeI,null,dialog_sign));
                fragments.add(MusiclikeListFragment.newInstance(serialNo,dialog_sign));
                type_length = tabs.length;
                break;
        }

        fragmentStatePagerAdapter = new FragmentStatePagerAdapter(mFragmentManager, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @Override
            public int getCount() {
                return type_length;
            }

            @Override
            public int getItemPosition(@NonNull Object object) {
                return PagerAdapter.POSITION_NONE;
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
        };

        music_view_pager.setAdapter(fragmentStatePagerAdapter);
        music_view_pager.setCurrentItem(0);
    }

    @Override
    protected void initData() {
    }


    @Override
    public void onRightClick(View view) {
        super.onRightClick(view);
        Intent intent = new Intent();
        intent.putExtra("musicid",musicID);
        intent.putExtra("picUrl",musicPic);
        intent.putExtra("musicName",musicName);
        setResult(Activity.RESULT_OK,intent);
        finish();
    }


    //musiclistfragment的接口回调
    @Override
    public void sendValue(String music_id,String musicName1, String songpicurl) {
        if (music_id.length() < 4){
            musicID = music_id;
        }
//        musicID = music_id.substring(4);
        musicID = music_id;
        musicName = musicName1;
        musicPic = songpicurl;
    }

    @Override
    public void sendValue_text(String musicid,String musicName1, String songpicurl) {
        musicID = "migu" + musicid;
        musicName = musicName1;
        musicPic = songpicurl;
    }

    public void sendLikeValue(String musicid,String musicName1, String songpicurl){
        musicID = musicid;
        musicName = musicName1;
        musicPic = songpicurl;
    }
}